import {
    getApplicationtypedistribution,
    getApplypeoplesweat,
    getApprovaltimetakingdistribution,
    getCommonworkflowdistribution,
    getProcesspeopleSweat,
    getWorkflownumberdistribution,
    getWorkfloworgdistribution,
    getWorkflowstatusdistribution
} from '@/services/flowManage/statService';

export default {
    namespace: 'stat',
    state: {
        sqspryfx: {},
        sqfqrph: [],
        sphsfx: {},
        lcsprph: [],
        spsqlxfx: [],
        cylcph: [],
        lcztfx: [],
        fqgsbmph: []
    },
    effects: {
        *fetchAllData({ payload }, { all, put }) {
            yield all([
                put({
                    type: 'fetchSqspryfx',
                    payload
                }),
                put({
                    type: 'fetchSqfqrph',
                    payload
                }),
                put({
                    type: 'fetchSphsfx',
                    payload
                }),
                put({
                    type: 'fetchLcsprph',
                    payload
                }),
                put({
                    type: 'fetchSpsqlxfx',
                    payload
                }),
                put({
                    type: 'fetchCylcph',
                    payload
                }),
                put({
                    type: 'fetchLcztfx',
                    payload
                }),
                put({
                    type: 'fetchFqgsbmph',
                    payload
                })
            ]);
        },
        *fetchSqspryfx({ payload }, { call, put }) {
            const result = yield call(getWorkflownumberdistribution, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        sqspryfx: result.data
                    }
                });
            }
        },
        *fetchSqfqrph({ payload }, { call, put }) {
            const result = yield call(getApplypeoplesweat, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        sqfqrph: result.data
                    }
                });
            }
        },
        *fetchSphsfx({ payload }, { call, put }) {
            const result = yield call(
                getApprovaltimetakingdistribution,
                payload
            );

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        sphsfx: result.data
                    }
                });
            }
        },
        *fetchLcsprph({ payload }, { call, put }) {
            const result = yield call(getProcesspeopleSweat, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        lcsprph: result.data
                    }
                });
            }
        },
        *fetchSpsqlxfx({ payload }, { call, put }) {
            const result = yield call(getApplicationtypedistribution, {
                ...payload
            });

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        spsqlxfx: result.data
                    }
                });
            }
        },
        *fetchCylcph({ payload }, { call, put }) {
            const result = yield call(getCommonworkflowdistribution, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        cylcph: result.data
                    }
                });
            }
        },
        *fetchLcztfx({ payload }, { call, put }) {
            const result = yield call(getWorkflowstatusdistribution, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        lcztfx: result.data
                    }
                });
            }
        },
        *fetchFqgsbmph({ payload }, { call, put }) {
            const result = yield call(getWorkfloworgdistribution, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        fqgsbmph: result.data
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear() {
            return {
                sqspryfx: {},
                sqfqrph: [],
                sphsfx: {},
                lcsprph: [],
                spsqlxfx: [],
                cylcph: [],
                lcztfx: [],
                fqgsbmph: []
            };
        }
    }
};
